# -*- coding:utf8 -*- #
# -----------------------------------------------------------------------------------
# ProjectName:  smart-meter-iot-test-console
# FileName:     getSign.py
# Author  :
# Datetime:     2023/3/16 23:20
# Description：
# -----------------------------------------------------------------------------------
import calendar
import time
from common.util.configUtil.configUtil import ConfigUtil
from common.util.encryptionUtil.crc16Util import crc16_Modbus
from common.util.jsonUtl.jsonUtil import JsonUtil
from common.util.mySqlUtil.mySqlPool import select_sql

jsonUtil = JsonUtil()
configUtil = ConfigUtil()


def getSign(deviceName: str, timestamp: str) -> str:
    """
    通过组合的字符串获取crc16_Modbus校验码

    :return: 返回crc16_Modbus检验码
    """
    # 时间戳
    # timestamp = str(calendar.timegm(time.gmtime()))
    # 获取供应商秘钥
    if deviceName.startswith("C"):
        select_vendorKey_sql = jsonUtil.read_sql("select_collector_vendorKey").format(deviceName)
    else:
        select_vendorKey_sql = jsonUtil.read_sql("select_meter_vendorKey").format(deviceName)
    # 从数据库获取供应商秘钥
    vendorKey = select_sql(select_vendorKey_sql)[0][0]
    content = f'{deviceName}{configUtil.get_productkey()}{timestamp}{vendorKey}'
    # 加密
    getSign = crc16_Modbus(content)
    return getSign


if __name__ == '__main__':
    print(getSign("C40000000001", str(calendar.timegm(time.gmtime()))))
